// pages/Store/Store.js
var tool = require("../Tongyong/tongyong.js");//调取共用类的方法
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loadingHidden: false,
    dongtaiArray:[],
    visitor_id:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //调用数据更新方法
    this.getdatarequest();
  },


  /***更新数据方法 */
  getdatarequest:function(){
    
    var self = this;

    self.setData({
      loadingHidden: false,
    })
    
    var VID='';
    wx.getStorage({
      key: 'viewitorid',
      success: function (res) {
        VID = res.data;
          
        self.setData({
          visitor_id: VID
        })

        var qingqiudata = {
          is_page: 0,
          page: 1,
          pageSize: 1,
          visitor_id: VID
        }

        tool.Httprequest('getArtInfo', qingqiudata, function (res) {
          console.log(res);

          var dataarray = res.data;
          var mustarray = [];
          for (let i = 0; i < dataarray.length; i++) {
            var dongtaiitem = {
              content: '',
              art_id: '',
              title: '',
              isbofang: false,
              images: [],
              createtime: '',
              iszhankaiBUt: false,
              iszhankai: true,
              click_status: 0,
              click_num: 0,
            };
            dongtaiitem.content = dataarray[i].description;
            dongtaiitem.title = dataarray[i].title;
            dongtaiitem.createtime = dataarray[i].updatetime;
            dongtaiitem.art_id = dataarray[i].art_id;
            dongtaiitem.click_status = dataarray[i].click_status;
            dongtaiitem.click_num = dataarray[i].click_num;

            if (dataarray[i].thumb.length > 0) {
              dongtaiitem.isbofang = true;
              dongtaiitem.images.push(dataarray[i].thumb);
            } else {
              dongtaiitem.isbofang = false;
              dongtaiitem.images = dataarray[i].images;
            }

            //取出内容判断是否展开收起
            var b = tool.getString(dongtaiitem.content);
            if (b >= 100) {
              dongtaiitem.iszhankaiBUt = true;
              dongtaiitem.iszhankai = false;
            } else {
              dongtaiitem.iszhankaiBUt = false;//是否展示  展开按钮
              dongtaiitem.iszhankai = true;
            }

            mustarray.push(dongtaiitem);
          }

          self.setData({
            dongtaiArray: mustarray
          })

          // console.log(mustarray);

        }, function (res) {
          console.log(res);
        }, function (res) {

          wx.stopPullDownRefresh();
          self.setData({
            loadingHidden: true,
          });
        })

      }
    });

    
  },

  /***展开方法 */
  zhankaishouqiAction:function(e){
    console.log(e.currentTarget.dataset.index);
    var selectind = e.currentTarget.dataset.index;
    var self = this;
    var itemdic = self.data.dongtaiArray[selectind];
    if (itemdic.iszhankai==true){
      itemdic.iszhankai =false;
    }else{
      itemdic.iszhankai = true;
    }
    var dicarray = self.data.dongtaiArray;
    dicarray[selectind] = itemdic;
    self.setData({
      dongtaiArray: dicarray
    })

  },

  //点赞的时候出发
  getUserInfo: function (e) {
    console.log(e)
    var selectart_id = e.target.dataset.art_id;
    var selectindex = e.target.dataset.index;

    var self=this;
    
    var status=0;

    //判断是要点赞好还是取消点赞
    if (self.data.dongtaiArray[selectindex].click_status==1){
       //证明已经点赞过  现在是取消点赞

      status=0;

    }else{
      status=1;
    }
    var qingqiudata = {
      artcle_id: selectart_id,
      visitor_id: self.data.visitor_id,
      status: status
    }
    self.setData({
      loadingHidden: false,
    })
    tool.Httprequest('saveVisitClick', qingqiudata, function (res) {
      console.log(res);
      self.getdatarequest();
    }, function (res) {

    }, function (res) {
      self.setData({
        loadingHidden: true,
      });
    });

  },
  /***防止冒泡内容不实现 */
  kongfangfa:function(e){

  },
  /**   
     * 预览图片  
     */
  previewImage: function (e) {
    var current = e.target.dataset.src;
    var index = e.target.dataset.ind;//当前段落

    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: this.data.dongtaiArray[index].images // 需要预览的图片http链接列表  
    })
  },

  /***进入详情 */
  DetailsAction:function(e){
    console.log(e)
    var artid = e.currentTarget.dataset.namdeid;
    wx.navigateTo({
      url: '../DTDetails/DTDetails?art_id=' + artid + '&visitor_id=' + this.data.visitor_id,
    })
  },

  xunjiaAction: function (e) {
    wx.navigateTo({
      url: '../Xunjiajilu/Xunjiajilu',
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //调用数据更新方法
    this.getdatarequest();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})